package _25define;

public class Demo2 {
    public static void main(String[] args) {
        UpDate myrunable=new UpDate();
        Thread t1=new Thread(myrunable);
        Thread t2=new Thread(myrunable);
        Thread t3=new Thread(myrunable);
        t1.setName("线程1下载中");
        t2.setName("线程2下载中");
        t3.setName("线程3下载中");
        t1.start();
        t2.start();
        t3.start();
        try {
            t1.join();
            t2.join();
            t3.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("文件下载完成");

    }
}
class UpDate implements Runnable{
    int tickets=100;
    Object obj=new Object();
    @Override
    public void run() {
        while (true){
            synchronized (obj){
                if (tickets>0){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+"剩余"+(tickets--)+"%资源");
                }else break;
            }
        }
    }
}
